/**
 * 
 */
package kr.co.insoft.publicmesh.controller.template.application;

import kr.co.insoft.core.model.paging.PagingModel;
import kr.co.insoft.core.model.template.application.ApplicationEntityModel;
import kr.co.insoft.core.service.template.application.ApplicationService;
import kr.co.insoft.core.service.template.application.EventService;
import kr.co.insoft.core.service.template.application.ParamService;
import kr.co.insoft.core.service.template.script.ScriptService;
import kr.co.insoft.framework.exceptions.ServiceException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author IN-SOFT Inc. Park Jong Taek(jtpark@in-soft.co.kr)
 * 
 */
@Controller
@RequestMapping(value = "template/application")
public class ApplicationController {

	final String TILES_PAGE_PREFIX = "tiles/template/application";
	final String SINGLE_PAGE_PREFIX = "template/application";

	@Autowired
	ApplicationService applicationService; 
	
	@Autowired
	ScriptService scriptService;
	
	@Autowired
	EventService eventService;
	
	@Autowired
	ParamService paramService;
	
	/**
	 * Application 목록
	 * 
	 * @param model
	 * @param pagingModel
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "lists")
	public String lists(
			Model model,
			@ModelAttribute("pagingModel") PagingModel pagingModel,
			@RequestParam(value = "ip", defaultValue = "N", required = false) String ip)
			throws Exception {
		model.addAttribute("pagingModel", applicationService.getPagingList(pagingModel));
		
		if (ip.equals("Y"))
			return SINGLE_PAGE_PREFIX+"/lists";
		
		return TILES_PAGE_PREFIX + "/lists";
	}

	/**
	 * Application 등록화면
	 * 
	 * @param model
	 * @return
	 * @throws ServiceException 
	 */
	@RequestMapping(value = "createForm", method = RequestMethod.GET)
	public String createForm(
			Model model,
			@RequestParam(value = "ip", defaultValue = "N", required = false) String ip) throws ServiceException {
		model.addAttribute(new ApplicationEntityModel());
		model.addAttribute("applicationTypeList", applicationService.getTypeList());
		model.addAttribute("scriptList", scriptService.getList());
		
		if (ip.equals("Y"))
			return SINGLE_PAGE_PREFIX+"/createForm";
		
		return TILES_PAGE_PREFIX + "/createForm";
	}
	
	/**
	 * Application Name 중복 체크
	 * 
	 * @param ScriptEntityModel
	 * @return
	 */
	@RequestMapping(value = "isExistApplicationName", method = RequestMethod.POST)
	public @ResponseBody ResponseEntity<Boolean> isExistApplicationName(
			@ModelAttribute("applicationEntityModel") ApplicationEntityModel applicationEntityModel) {
		try {
			if (applicationService.isExistApplicationName(applicationEntityModel))
				return new ResponseEntity<Boolean>(true, HttpStatus.OK);
		} catch (ServiceException e) {
			return new ResponseEntity<Boolean>(false, HttpStatus.OK);
		}
		return new ResponseEntity<Boolean>(false, HttpStatus.OK);
	}
	
	/**
	 * Application 등록
	 * 
	 * @param applicatonEntityModel
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "createForm", method = RequestMethod.POST)
	public String createVariable(
			@ModelAttribute("applicatonEntityModel") ApplicationEntityModel applicatonEntityModel,
			@RequestParam(value = "ip", defaultValue = "N", required = false) String ip,
			final BindingResult bindingResult) throws Exception {
		
		if (bindingResult.hasErrors()) {
			if (ip.equals("Y"))
				return SINGLE_PAGE_PREFIX+"/createForm";
			
			return TILES_PAGE_PREFIX + "/createForm";
		}
	
		applicationService.createApplication(applicatonEntityModel);
		
		if (ip.equals("Y"))
			return "redirect:./lists?ip=Y";
		
		return "redirect:./lists";
	}
	
	/**
	 * Application 삭제
	 * 
	 * @param applicationid
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "remove", method = RequestMethod.POST)
	public @ResponseBody ResponseEntity<Boolean> remove(
			@RequestParam("applicationid") String applicationid) {		
		try {
			applicationService.doRemoveApplication(applicationid);
			return new ResponseEntity<Boolean>(true, HttpStatus.OK);
		} catch (ServiceException e) {
			return new ResponseEntity<Boolean>(false, HttpStatus.OK);
		}
	}
	
	/**
	 * Application 상세 화면
	 * 
	 * @param applicationid
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "view", method = RequestMethod.GET)
	public String view(
			Model model,
			@RequestParam("applicationid") String applicationid,
			@RequestParam(value = "ip", defaultValue = "N", required = false) String ip)
			throws Exception {
		
		model.addAttribute("applicationEntityModel", 
				applicationService.getApplication(applicationid));
				
		if (ip.equals("Y"))
			return SINGLE_PAGE_PREFIX + "/view";

		return TILES_PAGE_PREFIX + "/view";
	}
	
}
